﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Bloodbank.Api;
using Bloodbank.Dto;

namespace Bloodbank
{
    public partial class frmAdmin : Form
    {
        public frmAdmin()
        {
            InitializeComponent();
        }
        private static int UserId = -1;

        private void LoadGridUserList()
        {
            UserApi userApi = new UserApi();
            dgvUserlist.DataSource = userApi.GetUserList();            
        }

        private void LoadCbbGroupBlood()
        {
            BloodGroupApi bloodGroupApi = new BloodGroupApi();
            cbbBloodgroup.DisplayMember = "BloodGroupName";
            cbbBloodgroup.ValueMember = "Id";
            cbbBloodgroup.DataSource = bloodGroupApi.GetGroupBloodList();

            btnEdit.Enabled = false;
            btnDelete.Enabled = false;
        }

        private void frmAdmin_Load(object sender, EventArgs e)
        {
            dgvUserlist.AutoGenerateColumns = false;
            LoadCbbGroupBlood();
            LoadGridUserList();
            radMale.Checked = true;            
        }

        private Boolean TestInput()
        {
            if (txtUsername.Text.Trim() == string.Empty)
            {
                MessageBox.Show("Username is required", "Message");
                return false;
            }
            if (txtPassword.Text.Trim() == string.Empty)
            {
                MessageBox.Show("Password is required", "Message");
                return false;
            }
            if (txtFullname.Text.Trim() == string.Empty)
            {
                MessageBox.Show("Fullname is required", "Message");
                return false;
            }
            return true;
        }

        private void btnCreate_Click(object sender, EventArgs e)
        {
            if (TestInput())
            {
                CheckLoginDto checkLogin = new CheckLoginDto();
                CheckedLoginApi checkedLoginApi = new CheckedLoginApi();
                checkLogin = checkedLoginApi.CheckExistsUser(txtUsername.Text.Trim());
                if (checkLogin.IsSuccess)
                {
                    MessageBox.Show(checkLogin.Message, "Message");
                    return;
                }
                UserDto user = new UserDto()
                {
                    Username = txtUsername.Text.Trim(),
                    Password = txtPassword.Text.Trim(),
                    ConfirmPassword = txtPassword.Text.Trim(),
                    Email = txtEmail.Text.Trim(),
                    FullName = txtFullname.Text.Trim(),
                    Address = txtAddress.Text.Trim(),
                    City = txtCity.Text.Trim(),
                    State = txtState.Text.Trim(),
                    Gender = radMale.Checked ? true : false,
                    Phone = txtPhone.Text.Trim(),
                    Birthday = DateTime.Parse(dtpBirthday.Value.ToString()),
                    BloodGroupId = int.Parse(cbbBloodgroup.SelectedValue.ToString())
                };
                UserApi userApi = new UserApi();
                userApi.Inserted(user);
                MessageBox.Show("Account is created successfully", "Message");

                LoadGridUserList();
            }
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            txtUsername.Text = string.Empty;
            txtPassword.Text = string.Empty;
            txtFullname.Text = string.Empty;
            txtEmail.Text = string.Empty;
            txtAddress.Text = string.Empty;
            txtCity.Text = string.Empty;
            txtState.Text = string.Empty;
            radMale.Checked = true;
            radFreemale.Checked = false;
            txtPhone.Text = string.Empty;
            dtpBirthday.Value = DateTime.Now;
            cbbBloodgroup.SelectedIndex = 0;
            UserId = -1;

            btnCreate.Enabled = true;
            btnEdit.Enabled = false;
            btnDelete.Enabled = false;
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            UserDto user = new UserDto()
            {
                Id = UserId,
                Username = txtUsername.Text.Trim(),
                Password = txtPassword.Text.Trim(),
                ConfirmPassword = txtPassword.Text.Trim(),
                Email = txtEmail.Text.Trim(),
                FullName = txtFullname.Text.Trim(),
                Address = txtAddress.Text.Trim(),
                City = txtCity.Text.Trim(),
                State = txtState.Text.Trim(),
                Gender = radMale.Checked ? true : false,
                Phone = txtPhone.Text.Trim(),
                Birthday = DateTime.Parse(dtpBirthday.Value.ToString()),
                BloodGroupId = int.Parse(cbbBloodgroup.SelectedValue.ToString())
            };
            UserApi userApi = new UserApi();
            userApi.Updated(user);
            LoadGridUserList();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (UserId != -1 && MessageBox.Show("Are you sure to delete", "Message", MessageBoxButtons.OKCancel) == DialogResult.OK)
            {
                UserApi userApi = new UserApi();
                UserDto user = new UserDto();
                user = userApi.GetUserById(UserId);
                userApi.Deleted(user);
                LoadGridUserList();
                btnReset_Click(sender, e);
            }
        }

        private void dgvUserlist_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {              
        }

        private void dgvUserlist_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex != -1)
            {
                txtUsername.ReadOnly = true;
                int id = int.Parse(dgvUserlist.Rows[e.RowIndex].Cells[0].Value.ToString());
                UserDto user = new UserDto();
                UserApi userApi = new UserApi();
                user = userApi.GetUserById(id);
                if (user != null)
                {
                    txtUsername.Text = user.Username;
                    txtPassword.Text = user.Password;
                    txtFullname.Text = user.FullName;
                    txtEmail.Text = user.Email;
                    txtAddress.Text = user.Address;
                    txtCity.Text = user.City;
                    txtState.Text = user.State;
                    if ((bool)user.Gender)
                    {
                        radMale.Checked = true;
                    }
                    else
                    {
                        radFreemale.Checked = true;
                    }
                    txtPhone.Text = user.Phone;
                    dtpBirthday.Value = (DateTime)user.Birthday;
                    cbbBloodgroup.SelectedValue = user.BloodGroupId;
                }
                UserId = id;

                btnCreate.Enabled = false;
                btnEdit.Enabled = true;
                btnDelete.Enabled = true;
            }
        }

        private void logoutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
